package com.zhangck.bos2_0.action.transit;

import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.zhangck.bos2_0.Service.take_delivery.WayBillquickServices;
import com.zhangck.bos2_0.common.BaseAction;
import com.zhangck.bos2_0.dao.WayBillQuickRepoistory;
import com.zhangck.bos2_0.utils.FileUtils;

import cn.itcast.bos.domain.take_delivery.WayBill;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRPdfExporter;

/** 
* @author 上午茶
* @version 创建时间：2018年6月20日 下午12:23:44 
* 类说明 
*/
@ParentPackage("json-default")
@Namespace("/")
@Scope("prototype")
@Actions
@Controller
public class RxportAction extends BaseAction<WayBill>{
	
	@Autowired
	private WayBillquickServices wayBillquickServices;
	
	@Action(value="exportXlsBtn")
	public String exportXlsBtn() throws IOException{
	   List<WayBill> list= wayBillquickServices.findWayBills(model);
		
	   HSSFWorkbook workbook = new HSSFWorkbook();
	   HSSFSheet sheet = workbook.createSheet("运单数据");
	   
	   HSSFRow row = sheet.createRow(0);
	   row.createCell(0).setCellValue("运单号");
	   row.createCell(1).setCellValue("寄件人");
	   row.createCell(2).setCellValue("寄件人电话");
	   row.createCell(3).setCellValue("寄件人人地址");
	   row.createCell(4).setCellValue("收件人");
	   row.createCell(5).setCellValue("收件人电话");
	   row.createCell(6).setCellValue("收件人地址");
	   
	   for (WayBill wayBill : list) {
		   HSSFRow datarow = sheet.createRow(sheet.getLastRowNum()+1);
		   datarow.createCell(0).setCellValue(wayBill.getWayBillNum());
		   datarow.createCell(1).setCellValue(wayBill.getSendName());
		   datarow.createCell(2).setCellValue(wayBill.getSendMobile());
		   datarow.createCell(3).setCellValue(wayBill.getSendAddress());
		   datarow.createCell(4).setCellValue(wayBill.getRecName());
		   datarow.createCell(5).setCellValue(wayBill.getRecMobile());
		   datarow.createCell(6).setCellValue(wayBill.getRecAddress());
	   }
	   // 下载导出
		// 设置头信息
		ServletActionContext.getResponse().setContentType(
				"application/vnd.ms-excel");
		String filename = "运单数据.xls";
		String agent = ServletActionContext.getRequest()
				.getHeader("user-agent");
		filename = FileUtils.encodeDownloadFilename(filename, agent);
		ServletActionContext.getResponse().setHeader("Content-Disposition",
				"attachment;filename=" + filename);

		ServletOutputStream outputStream = ServletActionContext.getResponse()
				.getOutputStream();
		workbook.write(outputStream);
	   
		// 关闭
		workbook.close();

		return NONE;
	}
	
	@Action(value="exportXlsBtntwo")
	public String exportXlsBtntwo() throws IOException, TemplateException{
		Configuration configuration = new Configuration(Configuration.VERSION_2_3_22);
		
		configuration.setDirectoryForTemplateLoading(new File(
				ServletActionContext.getServletContext().getRealPath("/WEB-INF/template")));
		
		Template template = configuration.getTemplate("waybill.xml");
		
		List<WayBill> wayBills = wayBillquickServices.findWayBills(model);
		Map<String, Object> map = new HashMap<>();
		map.put("wayBillList", wayBills);
		
		String filename = "运单数据.xls";
		
		String agent = ServletActionContext.getRequest()
				.getHeader("user-agent");
		filename = FileUtils.encodeDownloadFilename(filename, agent);
		ServletActionContext.getResponse().setHeader("Content-Disposition",
				"attachment;filename=" + filename);
		
		ServletActionContext.getResponse().setContentType("application/vnd.ms-excel");
		
		template.process(map, new OutputStreamWriter(ServletActionContext.getResponse().getOutputStream(),"utf-8"));;
		return NONE;
	}
	
	@Action(value="exportXlsBtnPdf")
	public String exportXlsBtnPdf() throws IOException, JRException{
		
		List<WayBill> wayBills = wayBillquickServices.findWayBills(model);

		// 下载导出
		// 设置头信息
		ServletActionContext.getResponse().setContentType("application/pdf");
		String filename = "运单数据.pdf";
		String agent = ServletActionContext.getRequest()
				.getHeader("user-agent");
		filename = FileUtils.encodeDownloadFilename(filename, agent);
		ServletActionContext.getResponse().setHeader("Content-Disposition",
				"attachment;filename=" + filename);

		// 根据 jasperReport模板 生成pdf
		// 读取模板文件
		String jrxml = ServletActionContext.getServletContext().getRealPath(
				"/WEB-INF/jasper/waybill.jrxml");
		JasperReport report = JasperCompileManager.compileReport(jrxml);

		// 设置模板数据
		// Parameter变量
		Map<String, Object> paramerters = new HashMap<String, Object>();
		paramerters.put("company", "上午茶速运快递");
		// Field变量
		JasperPrint jasperPrint = JasperFillManager.fillReport(report,
				paramerters, new JRBeanCollectionDataSource(wayBills));
		
		System.out.println(wayBills);
		// 生成PDF客户端
		JRPdfExporter exporter = new JRPdfExporter();
		exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
		exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,
				ServletActionContext.getResponse().getOutputStream());
		exporter.exportReport();// 导出
		ServletActionContext.getResponse().getOutputStream().close();

		return NONE;
	
	}

}
